package com.jcfk.eam.controller.csm;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.*;
import com.jcfk.eam.domain.dto.csm.CsmWorkRecordDTO;
import com.jcfk.eam.domain.query.csm.CsmWorkRecordQuery;
import com.jcfk.eam.service.csm.CsmWorkRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
/**
 * 耗材操作记录查询
 *
 * @author yrl
 * @email
 * @date 2024-11-05
 */
@Api(tags = {"耗材操作记录查询管理"})
@RestController
@RequestMapping("csmworkrecord")
public class CsmWorkRecordController {
    @Autowired
    private CsmWorkRecordService csmWorkRecordService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<CsmWorkRecordQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询耗材操作记录查询")
    @PostMapping("/queryByPage")
    public RetPage<CsmWorkRecordDTO> queryByPage(@RequestBody ReqPageParam<CsmWorkRecordQuery> requestParam) {
        //设置默认排序
        if(requestParam.getSort().getOrderItem()==null){
            ReqSortParam<CsmWorkRecordQuery> sortParam=new ReqSortParam();
            sortParam.setColumn("lastUpdateTime");
            sortParam.setOrderBy("desc");
            requestParam.setSort(sortParam);
        }
        DataPage<CsmWorkRecordDTO> page = new DataPage<CsmWorkRecordDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            CsmWorkRecordDTO.class);
        page = csmWorkRecordService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     *  耗材操作记录查询
     * 根据id 查询详情
     */
    @ApiOperation("查询耗材操作记录查询")
    @GetMapping("/get/{id}")
    public RetResult<CsmWorkRecordDTO> get(@PathVariable("id") String id) {
            CsmWorkRecordDTO item = csmWorkRecordService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理耗材操作记录查询
     */
    @ApiOperation("保存耗材操作记录查询")
    @PostMapping("/save")
    @RequestLogHeader(modular = "耗材操作记录查询管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid CsmWorkRecordDTO csmWorkRecord) {
        // 数据校验
        RetResult ret = csmWorkRecordService.isValid(csmWorkRecord);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(csmWorkRecord.getId())){
            // 新增
            csmWorkRecordService.insert(csmWorkRecord);
        } else{
            // 更新
            csmWorkRecordService.update(csmWorkRecord);
        }
        return RetResult.ok();

    }

    /**
     *  批量删除耗材操作记录查询
     */
    @ApiOperation("批量删除耗材操作记录查询")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "耗材操作记录查询管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody CsmWorkRecordDTO csmWorkRecord) {
        return RetResult.ok(csmWorkRecordService.logicDeleteList(csmWorkRecord));
    }





    /**
     *  删除耗材操作记录查询
     */
    @ApiOperation("删除耗材操作记录查询")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "耗材操作记录查询管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        csmWorkRecordService.deleteItem(id);
        return RetResult.ok();
    }
}
